﻿/*
* @Author： whg
* @Date : 2021-1-19
* @Desc : 
*/
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;
using XNode;
[CreateAssetMenu(menuName = "StoryGraph/Graph",order = 0)]
public class StoryGraph : NodeGraph 
{
    List<StoryVoNode> storyVoNodeList = new List<StoryVoNode>();
    List<BaseNode> baseNodeList = new List<BaseNode>();
    public override void ExportData()
    {
        storyVoNodeList.Clear();
        for (int i = 0; i < nodes.Count; i++)
        {
            StoryVoNode node= nodes[i] as StoryVoNode;
            if(node!=null) 
            {
                if (node.index > 0)
                {
                    storyVoNodeList.Add(node);
                } 
            }
        }
        storyVoNodeList.Sort((x, y) => x.index.CompareTo(y.index));
        CreateStoryLua(storyVoNodeList);
    }
    void GetTableList(Node baseNode)
    {
        if (baseNode == null) return;
        BaseNode node = baseNode as BaseNode;
        if (node != null) 
        {
            foreach (NodePort item in node.Outputs)
            {
                foreach (var port in item.GetConnections())
                {
                    BaseNode next_node = port.node as BaseNode;
                    if (next_node != null)
                    {
                        if (!baseNodeList.Contains(next_node))
                            baseNodeList.Add(next_node);
                    }
                }
                foreach (var port in item.GetConnections())
                {
                    BaseNode next_node = port.node as BaseNode;
                    if (next_node != null)
                    {
                        GetTableList(next_node);
                    }
                }
            }
        }
    }
    List<BaseNode> GetContentNodeList(bool isCondit)
    {
        List<BaseNode> conditionList = new List<BaseNode>();
        for (int i = 0; i < baseNodeList.Count; i++)
        {
            ConditionNode node = baseNodeList[i] as ConditionNode;
            if (node != null && isCondit)
            {
                conditionList.Add(baseNodeList[i]);
            }else if(!isCondit && node == null)
            {
                conditionList.Add(baseNodeList[i]);
            }
        }
        return conditionList;
    }
    public void CreateStoryLua(List<StoryVoNode> nodeList)
    {
        StringBuilder sb = new StringBuilder();
        string LuaName = nodeList[0].graph.name;
        string path = Application.dataPath + "/Lua/config/config_story/test/" + LuaName + ".lua";
        CreatePath(Path.GetPathRoot(path));
        if (File.Exists(path)) File.Delete(path);
        sb.AppendFormat("Config = Config or {{}}\n");
        sb.AppendFormat("Config.{0}", LuaName);
        sb.Append(" = \n{\n");
        sb.Append("	sceneId = " + LuaName.Substring(5)+",\n");
        sb.Append("	progress = 80,\n");
        sb.Append("	storyVoList = \n	{");
        ToWrite(path,sb);
        sb.Clear();
        for (int i = 0; i < nodeList.Count; i++)
        {
            sb.Append("		[" + (i + 1) + "] = \n		{");
            //storyVoList
            sb.Append("\n"+nodeList[i].ToTable());
            baseNodeList.Clear();
            GetTableList(nodeList[i]);
            List<BaseNode> ConditList  =  GetContentNodeList(true);
            //condition
            for (int j = 0; j < ConditList.Count; j++)
            {
                sb.Append("\n" + ConditList[j].ToTable());
            }
            ConditList.Clear();
            ConditList = GetContentNodeList(false);
            //contentList
            if (ConditList.Count>0)
            {
                sb.Append("\n			contentList = " + "\n			{");
                for (int j = 0; j < ConditList.Count; j++)
                {
                    sb.Append("\n" + ConditList[j].ToTable());
                }
                sb.Append("\n			},");
            }
            sb.Append("\n		},");
            ToWrite(path,sb);
            sb.Clear();
        }
        sb.Append("\n	}");
        sb.Append("\n}");
        ToWrite(path,sb);
        EditorUtility.DisplayDialog("提示", "剧情配置"+LuaName+"生成成功", "确定");
        AssetDatabase.Refresh();
    }
    void CreatePath(string Path)
    {
        string[] dirs = Path.Split('/');
        StringBuilder fullPath = new StringBuilder();
        foreach (string folder in dirs)
        {
            if (folder.Contains(".")) break;
            fullPath.Append(folder);
            if (!Directory.Exists(fullPath.ToString())) Directory.CreateDirectory(fullPath.ToString());
            fullPath.Append("/");
        }
    }
    public static void ToWrite(string path, StringBuilder sb)
    {
        try
        {
            if (!System.IO.File.Exists(path))
            {
                FileStream stream = System.IO.File.Create(path);
                stream.Close();
                stream.Dispose();
            }
            using (StreamWriter writer = new StreamWriter(path, true))
            {
                writer.WriteLine(sb.ToString());
            }
        }
        catch {}
    }
}
